

import LSD_line_segment  as LSD_x
import cv2
import numpy as np
import matplotlib.pylab as plt
import LSD

#################################################################
##test
img = cv2.imread('../misc_pic/chairs.pgm')
#cv2.imwrite("../misc_pic/empire.pgm",img)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
gray=np.double(gray)
lsd=LSD_x.LSD_x()

scale = 0.8  # 图像的缩放比例
sigma_scale = 0.6
n_bins = 1024  # 梯度伪序的bin数量
ang_thresh = 22.5  # 角度阀值
log_eps = 0.0  # 阀值: -log(NFA)>log_eps
quant = 2.0  # 受限于梯度范数上的量化误差
density_thresh = 0.7  # 判断矩形对齐点密度的阀值
#rect_list=lsd.LineSegmentDetection(gray)
rect_list=LSD.LineSegmentDetection(gray,scale,sigma_scale,ang_thresh,density_thresh,quant,n_bins,log_eps)

out_image=np.zeros(gray.shape[:2])

print("line segment=%d"%len(rect_list))

for rect in rect_list:
    p1=(np.int(np.ceil(rect.x1)),np.int(np.ceil(rect.y1)) )
    p2=(np.int(np.ceil(rect.x2)),np.int(np.ceil(rect.y2)) )
    cv2.line(out_image,p1,p2,(255,255,255))

"""
for r in range(height):
    for c in range(width):
        print(grad_angles[r,c],end='\t')
    print()

plt.gray()
plt.subplot(121)
plt.imshow(used,interpolation="nearest")
plt.subplot(122)
plt.imshow(test,interpolation="nearest")
plt.show()q
"""
plt.imshow(out_image,interpolation="nearest")
plt.show()


while(True):
    cv2.imshow('corners',out_image)
    if cv2.waitKey() & 0xff==ord("q"):
        break